package com.imu.purse.admin.controller.business;

import java.util.List;
import java.util.Map;

import com.imu.purse.modules.app.entity.ImuUserEntity;
import com.imu.purse.modules.app.service.ImuUserLogService;
import com.imu.purse.modules.app.service.ImuUserService;
import com.imu.purse.modules.sys.entity.SysUserEntity;
import com.imu.purse.modules.trade.service.FundService;
import com.xiaoleilu.hutool.bean.BeanUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.imu.common.validator.ValidatorUtils;
import com.imu.common.validator.group.AddGroup;
import com.imu.common.annotation.SysLog;
import com.imu.purse.modules.trade.entity.FundMergeDetailEntity;
import com.imu.purse.modules.trade.service.FundMergeDetailService;
import com.imu.common.utils.R;

/**
 * 用户资金合并详情表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-06 19:40:01
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@RestController
@RequestMapping("fundmergedetail")
public class FundMergeDetailController extends com.imu.purse.modules.sys.controller.AbstractController
{
	/****
	 * 查询用户资金合并详情表
	 * @param params
 	 * @date 2018-07-06 19:40:01
	 * @return
	 */
	@SysLog("查询用户资金合并详情表")
	@GetMapping("/list")
	@RequiresPermissions("fundmergedetail:list")
	public R list(@RequestParam Map<String, Object> params)
	{
		R r = R.ok();
		if(CollectionUtils.isEmpty(params)) return r;
		
		FundMergeDetailEntity dto = BeanUtil.mapToBean(params, FundMergeDetailEntity.class, true);
		List<FundMergeDetailEntity> dtos = service.queryList(dto);
		if(CollectionUtils.isEmpty(dtos)) return r;
		
		return r.put("page", new PageInfo<FundMergeDetailEntity>(dtos));
	}
	
	/****
	 * 根据ID查询用户资金合并详情表
	 * @param id
 	 * @date 2018-07-06 19:40:01
	 * @return
	 */
	@SysLog("根据ID查询用户资金合并详情表")
	@GetMapping("/info/{id}")
	@RequiresPermissions("fundmergedetail:info")
	public R info(@PathVariable("id") Long id)
	{
		return R.ok().put("dto", service.findById(id));
	}
	
	/****
	 * 新增用户资金合并详情表
	 * @param dto
 	 * @date 2018-07-06 19:40:01
	 * @return
	 */
	@SysLog("新增用户资金合并详情表")
	@PutMapping("/mergeFund")
	@RequiresPermissions("fundmergedetail:mergeFund")
	public R mergeFund(@RequestBody FundMergeDetailEntity dto)
	{
		ValidatorUtils.validateEntity(dto, AddGroup.class);
		String remark=dto.getRemark();
		dto.setRemark(null);
		//校验转出ID是否存在
		ImuUserEntity imuUserEntity = imuUserService.findById(dto.getPayUserId());
		if(imuUserEntity == null){
			return R.error(0,"转出手机号码不存在");
		}
		if(!dto.getPayPhone().equals(imuUserEntity.getPhone())){
			return R.error(0,"用户信息不匹配");
		}
		//校验目的账号是否存在
		imuUserEntity = imuUserService.findUserByPhone(dto.getRecPhone());
		if(imuUserEntity == null){
			return R.error(0,"目的手机号不存在");
		}

		dto.setRecUserId(imuUserEntity.getId());
		dto.setOrderTime(System.currentTimeMillis());
		dto.setCreateUser(this.getUserId());
		if(fundService.mergeFund(dto) == 1){
			service.save(dto);
		}else{
			return R.error(0,"操作异常");
		}
		String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();

		userLogService.insertUserLog(dto.getId().intValue(),username,dto.getPayPhone(), com.imu.constant.Constant.UserLog.MONEY_CHANGE,remark);

		return R.ok();
	}
	
	@Autowired
	private FundMergeDetailService service;

	@Autowired
	private ImuUserService imuUserService;

	@Autowired
	private FundService fundService;

	@Autowired
	private ImuUserLogService userLogService;
}
